#include <bits/stdc++.h>
#define int long long
#define PII pair<int,int>
#define endl "\n"
#define LL long long
#define fi first
#define se second
#define debug(a) cout<<#a<<"="<<a<<endl;
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define sz(x) (int)x.size()
#define rd(x, y) rand() % (y - x + 1) + x
#define ls u << 1
#define rs u << 1 | 1
using namespace std;

const int N = 300010;
int n, m, x, y;

void solve() {
    cin >> n >> m >> x >> y;
    int ans = 0;
    if (!n && !m) {
        ans = 0;
    } else if (!n) {
        if (x > y) ans = x;
        else ans = y * m;
    } else if (!m) {
        ans = max(x, y) + y * (n - 1);
    } else if (x <= y) {
        ans = y * (n + m);
    } else {
        if (y == 0) {
            ans = x;
        } else {
            int cnt = (x + y - 1) / y;
            cnt--;
            if (cnt <= n) ans = (n - cnt + m) * y + x;
        }
        ans = max(y * n + x, ans);
    }
    cout << ans << endl;
}

signed main() {
    int tt = 1;
    cin >> tt;
    while (tt--) {
        solve();
    }
    return 0;
}
